/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.knet.sp.ncrm.modules.mywork.potentialcustomer.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.knet.sp.ncrm.common.persistence.AjaxMessage;
import cn.knet.sp.ncrm.common.utils.StringUtils;
import cn.knet.sp.ncrm.common.web.BaseController;
import cn.knet.sp.ncrm.modules.customer.potentialgroup.entity.PotentialGroup;
import cn.knet.sp.ncrm.modules.customer.potentialgroup.service.PotentialGroupService;
import cn.knet.sp.ncrm.modules.sys.utils.UserUtils;

/**
 * 分组Controller
 *
 * @author 冯亚鹏
 * @version 2015-08-24
 */
@Controller
@RequestMapping(value = "${adminPath}/mywork/potentialCustomer/potentialGroup")
public class PotentialGroupController extends BaseController {

	@Autowired
	private PotentialGroupService potentialGroupService;

	@ModelAttribute
	public PotentialGroup get(@RequestParam(required = false) String id) {
		PotentialGroup entity = new PotentialGroup();
		entity.setUser(UserUtils.getUser());
		if (StringUtils.isNotBlank(id)) {
			entity.setId(id);
			entity = potentialGroupService.get(entity);
		}
		if (entity == null) {
			entity = new PotentialGroup();
		}
		return entity;
	}

	@RequiresPermissions("mywork:potentialCustomer:potentialGroup:view")
	@RequestMapping(value = {"index", ""})
	public String index() {
		return "modules/mywork/potentialcustomer/potentialGroupList";
	}


	@RequiresPermissions("mywork:potentialCustomer:potentialGroup:view")
	@RequestMapping(value = {"list"})
	@ResponseBody
	public List<PotentialGroup> list(PotentialGroup potentialGroup, HttpServletRequest request,
									 HttpServletResponse response, Model model) {
		List<PotentialGroup> list = potentialGroupService.findList(potentialGroup, true);
		return list;
	}

	@RequiresPermissions("mywork:potentialCustomer:potentialGroup:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxMessage save(PotentialGroup potentialGroup, Model model, RedirectAttributes redirectAttributes) {
		if (StringUtils.isBlank(potentialGroup.getName())) {
			return AjaxMessage.error("分组名称不能为空");
		}
		try {
			potentialGroupService.save(potentialGroup);
			AjaxMessage ajaxMessage = AjaxMessage.success("保存分组成功");
			return ajaxMessage;
		} catch (Exception e) {
			logger.error("保存分组失败");
			AjaxMessage ajaxMessage = AjaxMessage.error(e.getMessage());
			return ajaxMessage;
		}
	}


	@RequiresPermissions("mywork:potentialCustomer:potentialGroup:edit")
	@RequestMapping(value = "update")
	@ResponseBody
	public AjaxMessage update(PotentialGroup potentialGroup, Model model, RedirectAttributes redirectAttributes) {
		if (StringUtils.isBlank(potentialGroup.getName())) {
			return AjaxMessage.error("分组名称不能为空");
		}
		try {
			potentialGroupService.save(potentialGroup);
			AjaxMessage ajaxMessage = AjaxMessage.success("修改分组成功");
			return ajaxMessage;
		} catch (Exception e) {
			logger.error("修改分组失败");
			AjaxMessage ajaxMessage = AjaxMessage.error(e.getMessage());
			return ajaxMessage;
		}
	}

	@RequiresPermissions("mywork:potentialCustomer:potentialGroup:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxMessage delete(PotentialGroup potentialGroup, RedirectAttributes redirectAttributes) {
		potentialGroupService.delete(potentialGroup);
		AjaxMessage ajaxMessage = AjaxMessage.success("删除分组成功");
		return ajaxMessage;
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(PotentialGroup potentialGroup,@RequestParam(required = false) String extId,
											  @RequestParam(required = false) Boolean addDefault,
											  HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<PotentialGroup> list = potentialGroupService.findList(potentialGroup, addDefault);
		for (int i = 0; i < list.size(); i++) {
			PotentialGroup e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId != null && !extId.equals(e.getId())
											   && e.getParentIds().indexOf("," + extId + ",") == -1)) {
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}

}